<?php

 /**
  * @package JooS
  * @subpackage Syntax_PHPackager
  */

  require_once "JooS/Syntax/Complex/Transition.php";

 /**
  * Variable ::= "$", Variable_Direction, Variable_Path;
  */
  class JooS_Syntax_PHPackager_Variable_Syntax extends JooS_Syntax_Complex_Transition {
    /**
     * @return string
     */
    public function compile() {
      $ret = parent::compile();

      if ($ret !== false) {
        require_once "JooS/Namespace.php";
        require_once "JooS/Syntax/PHPackager/Variable/Direction.php";

        $prefixValue = $this->getParsedValue(1);
        $functionParams = array();

        switch ($prefixValue) {
          case "":
            $functionName = "";
            break;
          case (JooS_Namespace::DELIMITER):
            $functionName = "Root";
            break;
          default:
            $count = 0;
            $back = JooS_Syntax_PHPackager_Variable_Direction::VARIABLE_BACK . JooS_Namespace::DELIMITER;

            str_replace($back, "", $prefixValue, $count);
            array_push($functionParams, $count);

            $functionName = "Back";
            break;
        }

        $path = $this->getParsedValue(2);
        if ($path)
          array_push($functionParams, "'$path'");

        $ret = "\$this->get" . $functionName . "Variable(" . implode(",", $functionParams) . ")";
      }
      return $ret;
    }

    /**
     * @return JooS_Syntax_Complex_Concatenate
     */
    public function createRule() {
      $parser = $this->getParser();
      
      return $parser->createSyntax("Complex_Concatenate", array(
        $parser->createSyntax("Simple_Symbol", "$"),
        $parser->createSyntax("PHPackager_Variable_Direction"),
        $parser->createSyntax("PHPackager_Variable_Path")
      ));
    }
  }
